package com.huabo.fxgl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huabo.fxgl.entity.Risk;
import com.huabo.fxgl.mapper.RiskMapper;
import com.huabo.fxgl.mapper.RiskcategoryMapper;
import com.huabo.fxgl.service.IRiskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class RiskServiceImpl extends ServiceImpl<RiskMapper, Risk> implements IRiskService {
    @Autowired
    private RiskcategoryMapper riskcategoryMapper;
    @Override
    public IPage<Risk> getRiskList(IPage page, String parentId, Risk risk) {
        log.info("risk: " + risk);
        List<BigDecimal> ids = riskcategoryMapper.selectChildCatIds(parentId);
//        log.info("ids: " + ids);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("1",1);
        if (ids!=null && ids.size()!=0) {
            queryWrapper.in("r.riskcatid", ids);
        }
        if (StringUtils.isNotEmpty(risk.getRisknumber()))
            queryWrapper.like("r.risknumber", risk.getRisknumber());
        if (StringUtils.isNotEmpty(risk.getRiskname()))
            queryWrapper.like("r.riskname", risk.getRiskname());
        if(StringUtils.isNotEmpty(risk.getUnit()))
            queryWrapper.eq("r.unit", risk.getUnit());
        if(StringUtils.isNotEmpty(risk.getMemo())) {
            if ("asc".equalsIgnoreCase(risk.getMemo()))
                queryWrapper.orderByAsc("r.riskcreatedt");
            else
                queryWrapper.orderByDesc("r.riskcreatedt");

        }
        queryWrapper.orderByDesc("r.riskid");

        return baseMapper.selectPage1(page, queryWrapper);
    }
}

